import Vue from 'vue'
import Router from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Dashboard from '../views/Dashboard.vue'
import store from '@/script/store'
import i18n from '@/i18n/i18n'

import { Message } from 'element-ui'
import { getItem } from '../script/config/common'

const originalPush = Router.prototype.push

Router.prototype.push = function push(location) {
  return originalPush.call(this, location)
  // return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

const routers = new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/404',
      name: 'errorPage',
      component: () => import('../views/404.vue')
    },
    {
      path: '/dashboard',
      name: 'dashboard',
      component: Dashboard,
      children: [
        {
          path: '/home',
          name: 'home',
          component: Home
        },
        {
          path: '/template',
          name: 'template',
          component: () => import('../views/Template.vue')
        },
        {
          path: '',
          redirect: '/home'
        },
        {
          path: '/admin/sysuser/list',
          name: 'sysUser',
          component: () => import('../views/sysUser/List.vue')
        },
        {
          path: '/admin/purviewgroup/list',
          name: 'purviewGroup',
          component: () => import('../views/purviewGroup/List.vue')
        },
        {
          path: '/admin/purview/list',
          name: 'purview',
          component: () => import('../views/purview/List.vue')
        },
        {
          path: '/admin/richText/list',
          name: 'richText',
          component: () => import('../views/richText/List.vue')
        },
        {
          path: '/admin/i18n/list',
          name: 'i18n',
          component: () => import('../views/i18n/List.vue')
        },
        {
          path: '/admin/imageView/list',
          name: 'imageView',
          component: () => import('../views/imageView/List.vue')
        },
      ]
    },
    {
      path: '*',
      redirect: '/404'
    },
  ]
})
// 路由守卫，控制菜单权限
routers.beforeEach((to, from, next) => {
  // console.log('to', to)
  // console.log('from', from)
  // console.log('store.isLogin', store.state.isLogin)
  let user = getItem('user');
  let navResource = getItem('navResource') || [];
  let loginUrl = getItem('loginUrl');
  let canTo = false; // 有权限可以跳转
  console.log('navResource', navResource);
  console.log("getCanTo ~ to.path:", to.path)
  let getCanTo = (navResource) => {
    for (const nav of (navResource || [])) {
      if (nav.PageUrl == to.path) {
        canTo = true;
      }
      if (nav.Childs && nav.Childs.length > 0) {
        getCanTo(nav.Childs)
      }
    }
  }
  // console.log("navResource", navResource)


  // 登录页面去除权限
  if (to.path === '/login') {
    if (user && user.SysUserId && loginUrl) {
      next(loginUrl);
      return;
    }
    next();
    return;
  }

  // 未登录情况下跳到登录页
  if (!user || !user.SysUserId) {
    next('/login');
    return;
  }

  // 校验菜单是否有权限
  getCanTo(navResource);
  // console.log("canTo", canTo)
  if (canTo) {
    next()
  } else if (!from.name) {
    next('/login')
  } else if (to.path == '/404') {
    Message.warning(i18n.messages[i18n.locale].noAccessRoute || '无权限访问,当前路由不存在');
    next(false);
    // next()
  } else {
    Message.warning(i18n.messages[i18n.locale].noAccess || '无权限访问');
    next(false);
  }
})


export default routers
